#include<stdio.h>
#include<fcntl.h>
#include<string.h>
#include<stdlib.h>
#include<malloc.h>
#include "../include/meta.h"
#include "../include/vfs_errorcodes.h"
char* filepath, *filename;

char * add_file( char* dest_path, char* file_name,char* src_path)
{
	char result[100];
	
	if(root==NULL)
	{	
		strcpy(result,"addfile_FAILURE ");
		strcat(result,ERR_VFS_ADDFILE_07);
		sprintf(str, "%s",result);
		return str;
	}
	
	if((strcmp(dest_path,"")==0) || (strcmp(file_name,"")==0) || (strcmp(src_path,"")==0))
	{
		strcpy(result,"addfile_FAILURE ");
		strcat(result,ERR_VFS_ADDFILE_00);
		sprintf(str,"%s",result);
		return str;
	}

	int d=0;
	while(d<strlen(file_name))
	{
		if(file_name[d]=='/')
		{
			strcpy(result,"addfile_FAILURE ");
			strcat(result,ERR_VFS_ADDFILE_02);
			sprintf(str, "%s",result);
			return str;
		}
		d++;
	}
		
	char destpath2[100],destpath3[100],destpath4[100];
	strcpy(destpath2,dest_path); 
	strcpy(destpath4,dest_path); 
	strcat(dest_path,file_name);
	filepath=malloc(100);
	sprintf(filepath,"%s",dest_path);
	filename=malloc(100);
	sprintf(filename,"%s",file_name);
	

	int ret= bst_search(bstroot,dest_path);
	if(ret!=-1)
	{	
		strcpy(result,"addfile_FAILURE ");
		strcat(result,ERR_VFS_ADDFILE_03);
		sprintf(str, "%s",result);
		return str;
	}


	strcpy(destpath3,dest_path); 
	FILE *fp_src, *fp;
	//fp_src = fopen(src_path, "r");
	//if(fp_src==NULL)
	//	return ERR_VFS_ADDFILE_05;
	
	fp= fopen(mounted_vfs,"rb+");
	fp_src = fopen(src_path, "r");
	if(fp_src == NULL)
	{	
		strcpy(result,"addfile_FAILURE ");
		strcat(result,ERR_VFS_ADDFILE_05);
		sprintf(str, "%s",result);
		return str;
	}
	int sz;
	fseek(fp_src,0,SEEK_END);
	sz = ftell(fp_src);
	//printf("size:%d",sz);
	if(sz>1024)
	{	
		strcpy(result,"addfile_FAILURE ");
		strcat(result,ERR_VFS_ADDFILE_06);
		sprintf(str, "%s",result);
		return str;
	}
	
	fseek(fp_src, 0, SEEK_SET);
	char *token1,*token2;
	char buffer[1024];
	token1 = strtok(src_path,"/");
	while(token1!=NULL)
	{
		token2=token1;
		token1=strtok(NULL,"/");
	}
	token1 = strtok(token2,".");
	token2 =strtok(NULL,".");
	//if(strcmp(token2,"txt")!=0 || strcmp(token2,"dat")!=0 )
	//{
	//	return ERR_VFS_ADDFILE_03;
	//}
	//node *ax ;
	//printf("%s",dest_path);
	//ax = (node*)checkpath_returnnode(dest_path,root);
	
	//if(ax!=NULL)
	
	
	if(main_header_var->max_no_file_descriptors == main_header_var->no_used_file_descriptors)
		{
		strcpy(result,"addfile_FAILURE ");
		strcat(result,ERR_VFS_ADDFILE_04);
		sprintf(str, "%s",result);
		return str;
		}
	int i =0;
	//printf("outside\n");	
		int l;
		
//printf("%s  ",destpath2);
		
		//printf("destpath: %s\n",dest_path);
		insertIntoTree(dest_path,root,0);
	//	displayTree(root);
	//char *token1;
	//char buffer[1024];	
//	node *updatenode = insertIntoTree( ;
//if(updatenode== NULL)
	//	return ERR_VFS_UPDATEFILE_02;
	
	node *n= (node*)checkpath_returnnode(destpath3,root); 	
	int index = n->index;

	int block_no = fd[index].block_number;
	int offset = main_header_var->headersize + 1024*(block_no);
	fseek(fp,offset,SEEK_SET);
	int x=ftell(fp);	
	
	if(strcmp(fd[index].file_type,"text")==0)
	{
		char ch;
		while((ch= fgetc(fp_src))!=EOF)
		{		//printf("%c",ch);
				fputc(ch,fp);
		}
		fputc(EOF,fp);
	
		int size= ftell(fp_src); 
		fd[index].file_size=size; //printf("add: %d, %d, %d",index,size,fd[index].file_size);
	}
	else
	{
		int ch;
		while((ch= fgetc(fp_src))!=EOF)
		{		//printf("%c",ch);
				fputc(ch,fp);
		}
		fputc(EOF,fp);
	
		int size= ftell(fp_src); 
		fd[index].file_size=size;
	}
/*	
	char buffer1[1024];
	fseek(fp,x,SEEK_SET);
	fread(buffer1,1024,1,fp);
	printf("\n%s",buffer1);
	* */
	fclose(fp_src);
	fclose(fp);
	
	return "addfile_SUCCESS";
}

int getFileIndex()
{	//printf("@%s@ ",filepath);
int i,j;

	if(filepath[strlen(filepath)-1]!='/')
		strcat(filepath,"/");

for(i=0;i<main_header_var->max_no_file_descriptors;i++)
	{		
		if(fl[i].a == 0)
		{
			//printf("in fl%d\n",i);
			//l=i;
			fl[i].a = 1;
			strcpy(fd[i].location_path,filepath); 
			strcpy(fd[i].file_name,filename); //printf("#%s# ",fd[i].file_name);
			char *ext= strrchr(filename,'.');
			char *xtnsn= ext+1; //printf("#%s# ",xtnsn);
			if(ext==NULL)
				strcpy(fd[i].file_type,"file");
			else if(strcmp(xtnsn,"h")==0)
				strcpy(fd[i].file_type,"text");
			else if(strcmp(xtnsn,"txt")==0)
				strcpy(fd[i].file_type,"text");
			else if(strcmp(xtnsn,"jpg")==0)
				strcpy(fd[i].file_type,"image");
			else if(strcmp(xtnsn,"png")==0)
				strcpy(fd[i].file_type,"image");
			else if(strcmp(xtnsn,"exe")==0)
				strcpy(fd[i].file_type,"exe");
				
			int sz=main_header_var->max_no_file_descriptors;
			sz=(int) sz/1.3;
			for(j=0;j<sz;j++)
			{

				if(bl[j].b==0)
				{//printf("in bl%d\n",j);
					bl[j].b=1;
					break;
				}
			}
		//	printf("out of bl %d \n",j);
			fd[i].block_number = j ;
			fd[i].file_size = 0; 
			main_header_var->no_used_file_descriptors=main_header_var->no_used_file_descriptors+1;
			
			bstroot=(bstnode *)bst_insert(bstroot,fd[i].location_path,i);
			//displayTree(root);
			
			int hash_index=hash(fd[i].file_name);
			insert_into_list(hash_index,fd[i].file_name,fd[i].location_path);
			
			//printf("%d\n");
			break;
		}
		
	}
	return i;
}





